package com.ua.secure.role.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ua.common.model.PageResultBean;
import com.ua.common.model.SysModule;
import com.ua.common.model.SysOrg;
import com.ua.common.model.SysRole;
import com.ua.common.model.SysUser;
import com.ua.secure.org.service.OrgService;
import com.ua.secure.role.service.RoleService;




/**
 * 用户管理配置控制器
 * @author yangying
 * 2013-4-9
 */
@Controller
@RequestMapping("/secure/role/*")
public class RoleController {

	@Autowired(required=true)
	private OrgService orgService;
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("listRoles.html")
	public ModelAndView listRoles() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("role",new SysRole());
		mav.setViewName("secure/role/queryRole");
		return mav;
	}
	
	@RequestMapping("loadRoleOrgData.html")
	public @ResponseBody List<SysOrg> loadRoleOrgsData(){
		List<SysOrg> list = orgService.loadOrgsData();
		// 去掉parentSysOrg
		for(SysOrg m : list){
			m.setParentSysOrg(null);
		}
		return list;
	}
	
	
	@RequestMapping(value="loadAllRoles.html")
	public @ResponseBody PageResultBean loadAllRoles(Integer jtStartIndex,Integer jtPageSize,String orgId,String roleName){
		PageResultBean pr = new PageResultBean();
		try {
			SysRole role = new SysRole();
			role.setOrgId(orgId);
			role.setRoleName(roleName);
			role.setFromNumber(jtStartIndex);
			role.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(roleService.queryAllRoles(role));
			pr.setTotalRecordCount(roleService.roleCount(role));
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		
		return pr;
	}
	@RequestMapping(value="listRoleUser.html")
	public ModelAndView listRoleUser() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("secure/role/roleUser");
		return mav;
	}
	//加载用户列表
	
	@RequestMapping(value="loadRoleUsers.html")
	public @ResponseBody PageResultBean loadRoleUsers(Integer jtStartIndex,Integer jtPageSize,String roleId){
		PageResultBean pr = new PageResultBean();
		try {
			SysUser user = new SysUser();
			user.setRoleId(roleId);
			user.setFromNumber(jtStartIndex);
			user.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(roleService.roleUsers(user));
			pr.setTotalRecordCount(roleService.roleUsersTotal(user));
			
			List<SysModule> moduleList = roleService.getSysModulesByRoleId(roleId);
			if(moduleList!=null && moduleList.size()>0){
				StringBuffer ml = new StringBuffer();
				for(SysModule modules : moduleList){
					ml.append(modules.getModuleId()).append(",");
				}
				ml.deleteCharAt(ml.length()-1);
				ml.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		
		return pr;
	}
	
}
